/* in html. need jQuery

<style>.tmbRndImgClass { width:100px; border-radius:5px; }</style>
<style>#imgholder_tags {border:0px solid red; margin:1em 0 2em}</style>

<div id="tmb_r_img_holder">

script type='text/javascript'>
var tmbURLArray=[
'sensualtouching','wettalk',
'mariux', 'textmesomethingdirty', 'onlybombs', 'justsomesex',
'mmmfeelsgood', 'paradisehentai', 'sexoe-prazer',
'asialicious', 'fellatioxx', 'finefemaleart', 'buttercooky',
'dnwbdiay', 'gldslvr', 'altporn', 'jyunf', 'orientalbest',
'nsfwizard', 'rulittle2', 'nrshs', 'kuku1984', 'jphc20xx',
'fucked-from-behind', 'jpg0image', 'kitkhsh69', 'comitan',
'eternal-ero', 'melc', 'mondoporno','sexydollshouse',
'phucking-photons', 'primadonnaxxpussy' , 'slutsoft',
  // west //////////////////////////////////////////////
'easilytempted', 'asideofsex', 'holdyourorgasm', 'we-want-porn',
'sexygirlsandporn', 'always-arousedxxx', 'corta-pra-18', 'fuckmaker',
  // west gif //////////////////////////////////////////////
'baoxe', 'yummyporn', 'fuckmaker',
  // east //////////////////////////////////////////////
'avgirls', 's-m-rothschild', 'mygirl', 'sexlip', 'ibreast', 
'erotiqueeee', 'mar-bee', 'japansex', 'sexyasia',
'acmejapan', 'kotogahakugaka', 'iluvjav', 'chaaraao',
'east-to-shanghai', 'ero4', 'cleavageilike', 'oclif',
'asya', 'flammie01gif', 'flammie01', 'ystest',
'jaylovesasians',
  // hentai //////////////////////////////////////////////
'isitweirdifindcartoonssexy', 'milky-syrup', 'master-senpai',
'onichanfuckedme', 'tibitan', 'sloworhard', 'wontonart', 'province-of-hentalia', 
'rule34andstuff', 'ichigomilkero', 'womenfuckingmonsters',
'hentai-age','drawn-out-masturbation',
'sekkusudorei', 'ilovetesticles',
  // artistic //////////////////////////////////////////////
'loveslife2005', 'crossworm', 'sexygirlsandporn', 'hobart1240', 'pokies-nips',
  // in 00trashcan00
'ecchi-paradise', 'dxb-mania',
'digitalpinkcloud', 'asiaunwrapped', 'peachhipz', 'sukijav',
'badasianmuse', 'lovewhenyoutalkdirty','coreha',
'eropix', 'starplatinum', 'phroydianslip','olderoticart',
'pervertisland', 'tooloot', 'muchimuchi', 'myfetishism'
];



view_tmb_img_urlarr(250, 'tmb_r_img_holder', true ,'tmbRndImgClass');

</script>
*/

/// @param widthSize :  1280 500 400 250 100 75. undefine to usualy set 500
function view_tmb_img_urlarr(widthSize, imgDiv, isOnlyOneImage, imgClassName)
{
  var imgholder =  $('#' + imgDiv);
  
  // imgid element exist check
  if (imgholder.length  <= 0)
  {
      alert("imgholder undefined : " + imgDiv);
      return;
  }
  
  if( typeof tmbURLArray == 'undefined' )
  {
      //전역변수인 tmbURLArray 를 재설정 하기 위함. var 사용하지 말것.
      tmbURLArray= ['yaimg'];
  }
  if (typeof imgClassName == 'undefined')
  {
    imgClassName = 'tmbArrRndImgStyle';
  }
  
  // check width size param
  switch(widthSize)
  {
      case 1280 :
      case 500 :
      case 400 :
      case 250 :
      case 100 :
      case  75 :
          //console.log('widthSize fit');
          break;
      default :
          //console.log('widthSize -_-;;');
          widthSize = 0;
          break;
  }
  
  //var url = 'yaimg';
  var ith = Math.floor(Math.random() * tmbURLArray.length);
  _url = tmbURLArray[ith] ;
  
  var infoLink = "http://api.tumblr.com/v2/blog/" + _url + ".tumblr.com/info?";
  var photoLink = "http://api.tumblr.com/v2/blog/" + _url + ".tumblr.com/posts?";
  
  $.ajax({
      type: "GET",
      url : infoLink,
      dataType: "jsonp", 
      data: {
          // http://www.tumblr.com/oauth/apps :: FriiNoView OAuth consumer key:
          api_key: "Vw6ftsZVq4oUg9vK7ZGOKGThqGQ2xfsRr7cy0MyKYvEC4dot14",
      }
  }).done(function( data ) {
      try
      {
          _totalNo = data.response.blog.posts;
      }
      catch(err)
      {
          alert(err.message + " : " + infoLink);
          return;
      }
      
      // 0 ~ (totalNo-1) random
      var rnd = 0 + Math.floor(Math.random() * _totalNo) ;
      //console.log(rnd);
      
      $.ajax({
          type: "GET",
          url : photoLink,
          dataType: "jsonp", 
          data: {
              // http://www.tumblr.com/oauth/apps :: FriiNoView OAuth consumer key:
              api_key: "Vw6ftsZVq4oUg9vK7ZGOKGThqGQ2xfsRr7cy0MyKYvEC4dot14",
              limit:1,
              offset:rnd
          }
      }).done(function( data ) {
          $.each(data.response.posts, function(){
              var curUrl = this.post_url;
              var _photos = this.photos;
              if ( typeof _photos == 'undefined' )
              {
                  view_tmb_img_urlarr(widthSize, imgDiv, isOnlyOneImage, imgClassName);
                  return;
              }
  
              var imgIdx = 0;
              if (_photos.length > 1 && isOnlyOneImage )
              {
                  imgIdx = 0 + Math.floor(Math.random() * _photos.length) ;
              }

              function GetImageWidthUrl(photos)
              {
                  // get 250 width image url
                  var widthUrl = '';
                  if (widthSize != 0)
                  {
                      $.each(photos.alt_sizes, function(){
                          //console.log(this); //DEBUG print
                          if(this.width == widthSize)
                          {
                              widthUrl = this.url;
                              return false; //break  remarks) return true; //continue
                          }
                      });
                  }
                  if (widthUrl == '')
                  {
                      widthUrl = photos.alt_sizes[1].url
                  }
                  return widthUrl;
              }
              
              // clear prev images
              imgholder.empty();
              
              // show info
              imgholder.append('<p id="imgholder_tags">' + _url + ' (' + rnd + ' / ' + _totalNo + ')  &nbsp; &nbsp; ' + this.tags.join(" / ") + '</p>');

              $.each(_photos, function(){
                  var sizedUrl = '';
                  if (isOnlyOneImage)
                  {
                      sizedUrl = GetImageWidthUrl(_photos[imgIdx]);
                  }
                  else
                  {
                      sizedUrl = GetImageWidthUrl(this);
                  }
                  
                  // show image
                  imgholder.append('<img src="' + sizedUrl + '" class="' + imgClassName + '">');
                  
                  if (isOnlyOneImage)
                  {
                      return false; // break
                  }
              });
              // link
              imgholder.wrap(function() {
                  return '<a href="' + curUrl + '" target="_blank"/>';
              });

              return;
          });
      });
  });
}